package cn.bsuc.infirmary.web.controller;


import cn.bsuc.infirmary.domain.Systemdictionaryitem;
import cn.bsuc.infirmary.query.SystemDictionaryItemQueryObject;
import cn.bsuc.infirmary.service.ISystemdictionaryService;
import cn.bsuc.infirmary.util.JSONResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemdictionaryService systemdictionaryService;

    @RequiresPermissions(value = {"字典页面", "systemdictionary:list"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo, Model model) {
        if (qo.getParentId()>0) {
            //设置排序规则
            qo.setOrderBy("sequence desc");
            model.addAttribute("pageInfo", systemdictionaryService.selectQueryObjectItem(qo));
        }
        model.addAttribute("diction", systemdictionaryService.selectList());
        return "systemdictionaryitem/list";
    }

    @RequiresPermissions(value = {"字典删除", "systemdictionary:delete"}, logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        if (id != null) {
            systemdictionaryService.deleteItem(id);
        }
        return new JSONResult();
    }

    @RequiresPermissions(value = {"字典增新", "systemdictionary:saveOrUpdate"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Systemdictionaryitem entity) {
        systemdictionaryService.saveOrUpdateItem(entity);
        return new JSONResult();
    }
}
